package com.hw.frame.app;

import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.utils.KJLoger;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.hw.frame.api.LogicProccessor;
import com.hw.frame.model.Base;

/**
 * 如果请求返回的数据中 有错误，则将错误消息放入 T的errorMessage中，否则就正常解析content中的内容
 * @author 黄伟
 *
 * @param <T>
 */
public class AppHttpCallBack<T extends Base> extends HttpCallBack {
	private LogicProccessor<T> proccessor;
	 
	private Class<T> entityClass;
	public AppHttpCallBack(LogicProccessor<T> proccessor,Class<T> cls) {
		super();
		this.proccessor = proccessor;
		this.entityClass  = cls;
	}

	

	@Override
	public void onSuccess(String json) {
		Log.i("onSuccess",">>>>>>>>>>>>>>>"+json);
		T t=JSON.parseObject(json,entityClass);//解析原始数据
		if(t.hasError()){
			t.setErrorMessage(t.getMessage());
		}
		proccessor.proccess(t);
	}

	@Override
	public void onFailure(int errorNo, String strMsg) {
		try {
			T objT=entityClass.newInstance();
			objT.setError(1);
			objT.setErrorMessage(strMsg);
			proccessor.proccess(objT);
		} catch (Exception e) {
			KJLoger.debug("实例化"+entityClass+"错误！", e);
		} 
	
	}
	


	

}
